package com.yjx.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yjx.mapper.OwnerMapper;
import com.yjx.util.AssertUtil;
import com.yjx.vo.Building;
import com.yjx.service.BuildingService;
import com.yjx.mapper.BuildingMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
* @author 10657
* @description 针对表【building】的数据库操作Service实现
* @createDate 2023-05-11 15:54:48
*/
@Service
public class BuildingServiceImpl extends ServiceImpl<BuildingMapper, Building>
    implements BuildingService{
    @Autowired
    private BuildingMapper buildingDao;
    @Resource
    private OwnerMapper ownerMapper;
    @Override
    public PageInfo<Building> findBuildAll(int page, int pageSize, String numbers) {
        PageHelper.startPage(page,pageSize);
        List<Building> list=buildingDao.queryBuildAll(numbers);
        return new PageInfo<>(list);
    }

    @Override
    public IPage<Building> findListByPage(Integer page, Integer pageCount){
        IPage<Building> wherePage = new Page<>(page, pageCount);
        Building where = new Building();

        return   baseMapper.selectPage(wherePage, Wrappers.query(where));
    }

    @Override
    public int add(Building building){
        return baseMapper.insert(building);
    }

    @Override
    public int delete(Long id){
        return baseMapper.deleteById(id);
    }

    @Override
    public int updateData(Building building){
        return baseMapper.updateById(building);
    }

    @Override
    public Building findById(Long id){
        return  baseMapper.selectById(id);
    }

    @Override
    public Building queryBuildById(Integer buildId) {
        return buildingDao.queryBuildById(buildId);
    }

    @Override
    public boolean deleteByIds(Integer[] ids) {
        List<Integer> list = ownerMapper.queryOwnerBuilding();
        List<Integer> delList = Arrays.asList(ids);
        System.out.println(list);
        System.out.println(delList);
        if(Collections.disjoint(list,delList)) {

            int count=buildingDao.deleteBatchIds(delList);
            int len = delList.size();
            return (count==len);

        }else{
            AssertUtil.isTrue(Collections.disjoint(list, delList), "所选楼号仍存在户主！");
            return false;
        }
    }
}






